package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired

    private HttpSession session;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    //查询所有菜品
    @Override
    public List<Category> findAll() {
        List<Category> categoriesAllList = categoryMapper.findAll();
        return categoriesAllList;
    }

    //添加菜品
    @Override
    public ResultInfo categoryAdd(Category category) {
        //雪花id 创建id
        long id = IdUtil.createSnowflake(1, 1).nextId();
        category.setId(id);

        //存入创建 时间
//        category.setCreateTime(new Date());
//        category.setUpdateTime(new Date());
        //获取创建人信息

        Employee attribute = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        //存入创建id
        Long userid = attribute.getId();

        // 存入创建人信息
//        category.setCreateUser(userid);
//        category.setUpdateUser(userid);

        //  categoryMapper.categoryAdd(category);
        categoryMapper.insert(category);
        return ResultInfo.success();

    }

    //  修改菜品
    @Override
    public void categorUpdat(Category category) {
        //获取修改人信息
        Employee attribute = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
//        category.setUpdateUser(attribute.getId());
//        category.setUpdateTime(new Date());
        //   categoryMapper.categorUpdat(category);
        categoryMapper.updateById(category);

    }

    //删除菜品
    @Override
    public ResultInfo categoryDeletByid(Long id) {
        //查看套餐下有无菜品和套餐
        Integer dish = categoryMapper.findDish(id);
        if (dish > 0) {
            //  return ResultInfo.error("此套餐有菜品 删除失败");
            throw new CustomException("该分类下有菜品，无法删除");
        }
        Integer setmeal = categoryMapper.findSetmeal(id);
        //如果有就返回失败

        if (setmeal > 0) {
            //   return ResultInfo.error("此套餐有套餐 删除失败");
            throw new CustomException("该分类下有套餐，无法删除");
        }


        categoryMapper.categoryDeletByid(id);


        return ResultInfo.success();
    }


}
